{% set navigation_html %}
  {% if navigation is not empty %}
    <table class="navigation d-print-none">
      <tr>
        <td class="navigation_separator"></td>

        {% if navigation.pos and not navigation.is_showing_all %}
          <td>
            <form action="{{ url('/sql') }}" method="post">
              {{ get_hidden_inputs(db, table) }}
              <input type="hidden" name="sql_query" value="{{ sql_query }}">
              <input type="hidden" name="pos" value="0">
              <input type="hidden" name="is_browse_distinct" value="{{ is_browse_distinct }}">
              <input type="hidden" name="goto" value="{{ goto }}">
              {% set begin_text %}
                {{- show_icons('TableNavigationLinksMode') ? '<<' }}
                {%- if show_text('TableNavigationLinksMode') -%}
                  &nbsp;{{ t('Begin', context = 'First page') }}
                {%- endif -%}
              {% endset %}
              <input type="submit" name="navig" class="btn btn-secondary ajax" value="{{ begin_text }}" title="{{ t('Begin', context = 'First page') }}">
            </form>
          </td>

          <td>
            <form action="{{ url('/sql') }}" method="post">
              {{ get_hidden_inputs(db, table) }}
              <input type="hidden" name="sql_query" value="{{ sql_query }}">
              <input type="hidden" name="pos" value="{{ navigation.pos_previous }}">
              <input type="hidden" name="is_browse_distinct" value="{{ is_browse_distinct }}">
              <input type="hidden" name="goto" value="{{ goto }}">
              {% set previous_text %}
                {{- show_icons('TableNavigationLinksMode') ? '<' }}
                {%- if show_text('TableNavigationLinksMode') -%}
                  &nbsp;{{ t('Previous', context = 'Previous page') }}
                {%- endif -%}
              {% endset %}
              <input type="submit" name="navig" class="btn btn-secondary ajax" value="{{ previous_text }}" title="{{ t('Previous', context = 'Previous page') }}">
            </form>
          </td>
        {% endif %}

        {{ navigation.page_selector|raw }}

        {% if not navigation.is_last_page %}
          <td>
            <form action="{{ url('/sql') }}" method="post">
              {{ get_hidden_inputs(db, table) }}
              <input type="hidden" name="sql_query" value="{{ sql_query }}">
              <input type="hidden" name="pos" value="{{ navigation.pos_next }}">
              <input type="hidden" name="is_browse_distinct" value="{{ is_browse_distinct }}">
              <input type="hidden" name="goto" value="{{ goto }}">
              {% set next_text %}
                {%- if show_text('TableNavigationLinksMode') -%}
                  {{ t('Next', context = 'Next page') }}&nbsp;
                {%- endif -%}
                {{ show_icons('TableNavigationLinksMode') ? '>' -}}
              {% endset %}
              <input type="submit" name="navig" class="btn btn-secondary ajax" value="{{ next_text }}" title="{{ t('Next', context = 'Next page') }}">
            </form>
          </td>

          {% if navigation.is_last_page_known %}
          <td>
            <form action="{{ url('/sql') }}" method="post"{{ navigation.onsubmit|raw }}>
              {{ get_hidden_inputs(db, table) }}
              <input type="hidden" name="sql_query" value="{{ sql_query }}">
              <input type="hidden" name="pos" value="{{ navigation.pos_last }}">
              <input type="hidden" name="is_browse_distinct" value="{{ is_browse_distinct }}">
              <input type="hidden" name="goto" value="{{ goto }}">
              {% set end_text %}
                {%- if show_text('TableNavigationLinksMode') -%}
                  {{ t('End', context = 'Last page') }}&nbsp;
                {%- endif -%}
                {{ show_icons('TableNavigationLinksMode') ? '>>' -}}
              {% endset %}
              <input type="submit" name="navig" class="btn btn-secondary ajax" value="{{ end_text }}" title="{{ t('End', context = 'Last page') }}">
            </form>
          </td>
          {% endif %}
        {% endif %}

        {% if navigation.number_total_page != 1 %}
          <td><div class="navigation_separator">|</div></td>
        {% endif %}

        {% if navigation.has_show_all %}
          <td>
            <form action="{{ url('/sql') }}" method="post">
              {{ get_hidden_fields(navigation.hidden_fields|merge({
                'session_max_rows': navigation.session_max_rows,
                'pos': '0'
              })) }}
              <input type="checkbox" name="navig" id="showAll_{{ unique_id }}" class="showAllRows" value="all"
                {{- navigation.is_showing_all ? ' checked' }}>
              <label for="showAll_{{ unique_id }}">{{ t('Show all') }}</label>
            </form>
          </td>
          <td><div class="navigation_separator">|</div></td>
        {% endif %}

        <td>
          <div class="save_edited hide">
            <input class="btn btn-link" type="submit" value="{{ t('Save edited data') }}">
            <div class="navigation_separator">|</div>
          </div>
        </td>
        <td>
          <div class="restore_column hide">
            <input class="btn btn-link" type="submit" value="{{ t('Restore column order') }}">
            <div class="navigation_separator">|</div>
          </div>
        </td>
        <td class="navigation_goto">
          <form action="{{ url('/sql') }}" method="post" class="maxRowsForm">
            {{ get_hidden_fields(navigation.hidden_fields|merge({
              'pos': navigation.pos,
              'unlim_num_rows': unlim_num_rows
            })) }}

            <label for="sessionMaxRowsSelect">{{ t('Number of rows:') }}</label>
            <select class="autosubmit" name="session_max_rows" id="sessionMaxRowsSelect">
              {% set rows_options = [25, 50, 100, 250, 500] %}
              {% if navigation.is_showing_all %}
                <option value="" disabled selected>{{ t('All') }}</option>
              {% elseif navigation.max_rows not in rows_options %}
                {% set rows_options = rows_options|merge([navigation.max_rows])|sort((a, b) => a <=> b) %}
              {% endif %}
              {% for option in rows_options %}
                <option value="{{ option }}"{{ navigation.max_rows == option ? ' selected' }}>{{ option }}</option>
              {% endfor %}
            </select>
          </form>
        </td>
        <td class="navigation_separator"></td>
        <td class="largescreenonly">
          <div class="row g-1 p-1 align-items-center">
            <div class="col-auto">
              <label for="filterRows" class="col-form-label">{{ t('Filter rows:') }}</label>
            </div>
            <div class="col-auto">
              <input type="text" id="filterRows" class="filter_rows form-control"
                    placeholder="{{ t('Search this table') }}" data-for="{{ unique_id }}">
            </div>
          </div>
        </td>
        <td class="largescreenonly">
          {% if navigation.sort_by_key is not empty %}
            <form action="{{ url('/sql') }}" method="post" class="d-print-none">
              {{ get_hidden_fields(navigation.sort_by_key.hidden_fields) }}
              {{ t('Sort by key:') }}
              <select name="sql_query" class="autosubmit">
                {% for option in navigation.sort_by_key.options %}
                  <option value="{{ option.value }}"{{ option.is_selected ? ' selected' }}>{{ option.content }}</option>
                {% endfor %}
              </select>
            </form>
          {% endif %}
        </td>
        <td class="navigation_separator"></td>
      </tr>
    </table>
  {% endif %}
{% endset %}

{{ sql_query_message|raw }}

{{ navigation_html }}

<input class="save_cells_at_once" type="hidden" value="{{ save_cells_at_once }}">
<div class="common_hidden_inputs">
  {{ get_hidden_inputs(db, table) }}
</div>

{% if headers.column_order is not empty %}
  {% if headers.column_order.order %}
    <input class="col_order" type="hidden" value="{{ headers.column_order.order|join(',') }}">
  {% endif %}
  {% if headers.column_order.visibility %}
    <input class="col_visib" type="hidden" value="{{ headers.column_order.visibility|join(',') }}">
  {% endif %}
  {% if not headers.column_order.is_view %}
    <input class="table_create_time" type="hidden" value="{{ headers.column_order.table_create_time }}">
  {% endif %}
{% endif %}

{% if headers.options is not empty %}
  <form method="post" action="{{ url('/sql') }}" name="displayOptionsForm" class="ajax d-print-none">
    {{ get_hidden_inputs({
      'db': db,
      'table': table,
      'sql_query': sql_query,
      'goto': goto,
      'display_options_form': 1
    }) }}

    {% if default_sliders_state != 'disabled' %}
    <div class="mb-3">
      <button class="btn btn-sm btn-secondary" type="button" id="extraOptionsButton" data-bs-toggle="collapse" data-bs-target="#extraOptions" aria-expanded="{{ default_sliders_state == 'open' ? 'true' : 'false' }}" aria-controls="extraOptions">
        {{ t('Extra options') }}
      </button>
    </div>
    <div class="collapse mb-3{{ default_sliders_state == 'open' ? ' show' }}" id="extraOptions">
    {% endif %}

      <div class="card">
        <div class="card-body">
          <div class="row">
            <fieldset class="col-auto">
              <div class="form-check">
                <input class="form-check-input" type="radio" name="pftext" id="partialFulltextRadioP{{ unique_id }}" value="P"{{ headers.options.pftext == 'P' ? ' checked' }}>
                <label class="form-check-label" for="partialFulltextRadioP{{ unique_id }}">{{ t('Partial texts') }}</label>
              </div>
              <div class="form-check">
                <input class="form-check-input" type="radio" name="pftext" id="partialFulltextRadioF{{ unique_id }}" value="F"{{ headers.options.pftext == 'F' ? ' checked' }}>
                <label class="form-check-label" for="partialFulltextRadioF{{ unique_id }}">{{ t('Full texts') }}</label>
              </div>
            </fieldset>

            {% if relwork and displaywork %}
              <fieldset class="col-auto">
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="relational_display" id="relationalDisplayRadioK{{ unique_id }}" value="K"{{ headers.options.relational_display == 'K' ? ' checked' }}>
                  <label class="form-check-label" for="relationalDisplayRadioK{{ unique_id }}">{{ t('Relational key') }}</label>
                </div>
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="relational_display" id="relationalDisplayRadioD{{ unique_id }}" value="D"{{ headers.options.relational_display == 'D' ? ' checked' }}>
                  <label class="form-check-label" for="relationalDisplayRadioD{{ unique_id }}">{{ t('Display column for relationships') }}</label>
                </div>
              </fieldset>
            {% endif %}

            <fieldset class="col-auto">
              <div class="form-check">
                <input class="form-check-input" type="checkbox" name="display_binary" id="display_binary_{{ unique_id }}"
                  {{- headers.options.display_binary is not empty ? ' checked' }}>
                <label class="form-check-label" for="display_binary_{{ unique_id }}">{{ t('Show binary contents') }}</label>
              </div>

              <div class="form-check">
                <input class="form-check-input" type="checkbox" name="display_blob" id="display_blob_{{ unique_id }}"
                  {{- headers.options.display_blob is not empty ? ' checked' }}>
                <label class="form-check-label" for="display_blob_{{ unique_id }}">{{ t('Show BLOB contents') }}</label>
              </div>
            </fieldset>

            {# I would have preferred to name this "display_transformation".
               This is the only way I found to be able to keep this setting sticky
               per SQL query, and at the same time have a default that displays
               the transformations. #}
            <div class="col-auto">
              <div class="form-check">
                <input class="form-check-input" type="checkbox" name="hide_transformation" id="hide_transformation_{{ unique_id }}"
                  {{- headers.options.hide_transformation is not empty ? ' checked' }}>
                <label class="form-check-label" for="hide_transformation_{{ unique_id }}">{{ t('Hide browser transformation') }}</label>
              </div>
            </div>

            <fieldset class="col-auto">
              {% if headers.options.possible_as_geometry %}
                <div class="form-check">
                  <input class="form-check-input" type="radio" name="geoOption" id="geoOptionRadioGeom{{ unique_id }}" value="GEOM"{{ headers.options.geo_option == 'GEOM' ? ' checked' }}>
                  <label class="form-check-label" for="geoOptionRadioGeom{{ unique_id }}">{{ t('Geometry') }}</label>
                </div>
              {% endif %}
              <div class="form-check">
                <input class="form-check-input" type="radio" name="geoOption" id="geoOptionRadioWkt{{ unique_id }}" value="WKT"{{ headers.options.geo_option == 'WKT' ? ' checked' }}>
                <label class="form-check-label" for="geoOptionRadioWkt{{ unique_id }}">{{ t('Well Known Text') }}</label>
              </div>
              <div class="form-check">
                <input class="form-check-input" type="radio" name="geoOption" id="geoOptionRadioWkb{{ unique_id }}" value="WKB"{{ headers.options.geo_option == 'WKB' ? ' checked' }}>
                <label class="form-check-label" for="geoOptionRadioWkb{{ unique_id }}">{{ t('Well Known Binary') }}</label>
              </div>
            </fieldset>
          </div>
        </div>

        <div class="card-footer">
          <input class="btn btn-primary" type="submit" value="{{ t('Go') }}">
        </div>
      </div>
    {% if default_sliders_state != 'disabled' %}
    </div>
    {% endif %}
  </form>
{% endif %}

{% if headers.has_bulk_actions_form %}
  <form method="post" name="resultsForm" id="resultsForm_{{ unique_id }}" class="ajax">
    {{ get_hidden_inputs(db, table) }}
    <input type="hidden" name="goto" value="{{ url('/sql') }}">
{% endif %}

  <div class="table-responsive-md">
    <table class="table table-striped table-hover table-sm table_results data ajax w-auto" data-uniqueId="{{ unique_id }}">
      <thead>
        <tr>

          {{ headers.button|raw }}
          {{ headers.table_headers_for_columns|raw }}
          {{ headers.column_at_right_side|raw }}

        </tr>
      </thead>

      <tbody>
        {{ body|raw }}
      </tbody>
    </table>
  </div>

{% if has_bulk_links is defined and has_bulk_links %}
    <div class="d-print-none">
      {{ get_image('select_all_arrow', t('With selected:'), {'dir': pma.text_dir, 'width': '38', 'height': '22'}) }}
      <input type="checkbox" id="resultsForm_{{ unique_id }}_checkall" class="checkall_box" title="{{ t('Check all') }}">
      <label for="resultsForm_{{ unique_id }}_checkall">{{ t('Check all') }}</label>
      <em class="with-selected">{{ t('With selected:') }}</em>

      <button class="btn btn-link mult_submit" type="submit" name="submit_mult" value="edit" title="{{ t('Edit') }}">
        {{ get_icon('b_edit', t('Edit')) }}
      </button>

      <button class="btn btn-link mult_submit" type="submit" name="submit_mult" value="copy" title="{{ t('Copy') }}">
        {{ get_icon('b_insrow', t('Copy')) }}
      </button>

      <button class="btn btn-link mult_submit" type="submit" name="submit_mult" value="delete" title="{{ t('Delete') }}">
        {{ get_icon('b_drop', t('Delete')) }}
      </button>

      {% if has_export_button %}
        <button class="btn btn-link mult_submit" type="submit" name="submit_mult" value="export" title="{{ t('Export') }}">
          {{ get_icon('b_tblexport', t('Export')) }}
        </button>
      {% endif %}
    </div>

    <input type="hidden" name="clause_is_unique" value="{{ clause_is_unique }}">
    <input type="hidden" name="sql_query" value="{{ sql_query }}">
  </form>
{% endif %}

{{ navigation_html }}

{% if operations is not empty %}
  <div class="card mb-3 d-print-none">
    <div class="card-header">{{ t('Query results operations') }}</div>

    <div class="card-body">
      {% if operations.has_print_link %}
        <button type="button" class="btn btn-link jsPrintButton">{{ get_icon('b_print', t('Print'), true) }}</button>

        {{ link_or_button(
          '#',
          null,
          get_icon('b_insrow', t('Copy to clipboard'), true),
          {'id': 'copyToClipBoard', 'class': 'btn'}
        ) }}
      {% endif %}

      {% if not operations.has_procedure %}
        {% if operations.has_export_link %}
          {{ link_or_button(
            url('/table/export'),
            operations.url_params,
            get_icon('b_tblexport', t('Export'), true),
            {'class': 'btn'}
          ) }}

          {{ link_or_button(
            url('/table/chart'),
            operations.url_params,
            get_icon('b_chart', t('Display chart'), true),
            {'class': 'btn'}
          ) }}

          {% if operations.has_geometry %}
            {{ link_or_button(
              url('/table/gis-visualization'),
              operations.url_params,
              get_icon('b_globe', t('Visualize GIS data'), true),
              {'class': 'btn'}
            ) }}
          {% endif %}
        {% endif %}

        <span>
          {{ link_or_button(
            url('/view/create'),
            {'db': db, 'table': table, 'sql_query': sql_query, 'printview': true},
            get_icon('b_view_add', t('Create view'), true),
            {'class': 'btn create_view ajax'}
          ) }}
        </span>
      {% endif %}
    </div>
  </div>
{% endif %}
{% if operations is not empty and not operations.has_procedure %}
{{ include('modals/create_view.twig') }}
{% endif %}
